package com.onlyphoto.onlymall.mvp.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.GoodsDetailsBean
import java.util.ArrayList

/**
 * Created by gaochujia on 2019-08-22.
 */

class SizeAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mPosition: Int = 0

    private var mList = arrayListOf<GoodsDetailsBean.Size>()
    private var listener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val item = LayoutInflater.from(parent.context).inflate(
                R.layout.item_style, parent, false)
        return SearchItemHolder(item)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as SearchItemHolder).loadData(mList[position])
        holder.itemView.setOnClickListener{
            mList[position].isSelected = !mList[position].isSelected

            if (mList[position].isSelected) {
                listener?.click(position, mList[position].name)
            } else {
                listener?.click(0, "")
            }
            if (mPosition != position) {
                mList[mPosition].isSelected = false
            }
            notifyItemChanged(position)
            notifyItemChanged(mPosition)
            mPosition = position
        }
    }

    fun setItemData(list: ArrayList<GoodsDetailsBean.Size>, isFirst: Boolean, sizeName: String) {
        this.mList = list
        if (sizeName == "" && isFirst) {
            mList[0].isSelected = true
        } else if (sizeName != ""){
            for ((index, size) in mList.withIndex()) {
                if (size.name == sizeName) {
                    mList[index].isSelected = true
                    mPosition = index
                }
            }
        }
        notifyDataSetChanged()
    }

    fun isChoose(): Boolean {
        for(size in mList) {
            if (size.isSelected) {
                return true
            }
        }
        return false
    }

    class SearchItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var content: TextView? = null

        init {
            content = itemView.findViewById(R.id.content)
        }

        fun loadData(size: GoodsDetailsBean.Size) {
            content?.text = size.name
            if (size.isSelected) {
                content?.setTextColor(ContextCompat.getColor(itemView.context, R.color.theme_color))
                content?.setBackgroundResource(R.drawable.style_selected)
            } else {
                content?.setTextColor(ContextCompat.getColor(itemView.context, R.color.text_color))
                content?.setBackgroundResource(R.drawable.style_normal)
            }
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.listener = listener
    }

    interface OnItemClickListener{
        fun click(position: Int, name: String)
    }
}